home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: do || die;
- Date: 7 Mar 1996 01:32:18 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hmaf2INNqir@keats.ugrad.cs.ubc.ca>
- References: <1996Mar7.052636.59812@ucl.ac.uk>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <1996Mar7.052636.59812@ucl.ac.uk>,
- Timothy Slidel <slidel@bsm.bioc.ucl.ac.uk> wrote:
- >Whilst it seems to work ok - is it considered bad style to use
- >logical operator expressions as conditional statements on their own, a la
- >Perl?
-
- Style is akin to taste and aesthetics. There are some rough guidelines, sort of
- like elusive artistic _faux pas_, which distinguish obfuscated C from readable,
- maintainable C.
-
- >e.g. if I want to decrement i only when it is != 0:
- >
- >i && i--;
- >
- >clearly something like:
- >
- >i && continue;
- >
- >won't work because continue is a keyword.
- >
- >I couldn't find anything about this in K&R2 or the C-FAQ and gcc -Wall just
-
- I did. I found out from the C grammar listed in the K&R that a the
- ``statement'' syntactic unit generates a ``logical-AND-expression ; ''
- sentential form quite happily. :)
-
- >issues a warning about an unused value...
-
- ...which is somewhat of an indicator that all may not be well, stylistically.
- However, it is valid syntax, and the behavior is well defined. The evaluation
- order of && is left to right, guaranteed, so it is clear that the decrementing
- side-effect takes place as a very last step, conceptually.
-
- Still, at first glance it appears as though undefined behavior is being
- invoked, until you remember the short-circuit evaluation of the && operator.
-
- It might seem confusing at first to someone not accustomed to dealing with
- Bourne shell scripts or Perl. But once you get used to seeing the construct, it
- becomes perspicuous.
-
- I think I did use it once or twice myself, with the || operator, but it is not
- likely that would have written that had I not previously learned Bourne shell
- scripting.
- --
-
-